﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Azure.TableQuery.Core;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace Azure.TableQuery.Tests
{

    [TestClass]
    public class GenericTableContextFixture : BaseTest
    {

        [TestMethod]
        public void GetFirstOrDefault()
        {
            var context = new GenericTableContext(account.TableEndpoint.ToString(), account.Credentials);

            var entity = context.GetFirstOrDefault("TestEntity");

            Assert.IsNotNull(entity);
            Assert.IsTrue(entity.GetProperties().Keys.Count > 0);
                        
            AssertPropertyExists(entity, "Name");
            AssertPropertyTypeIsEqual(entity, "Name", typeof(string));

            AssertPropertyExists(entity, "Age");
            AssertPropertyTypeIsEqual(entity, "Age", typeof(int));

            AssertPropertyExists(entity, "DateCreated");
            AssertPropertyTypeIsEqual(entity, "DateCreated", typeof(DateTime));            
        }

        private void AssertPropertyExists(GenericEntity entity, string propertyName)
        {
            Assert.IsTrue(entity.GetProperties().ContainsKey(propertyName));
        }

        private void AssertPropertyTypeIsEqual(GenericEntity entity, string propertyName, Type expected)
        {
            Assert.AreEqual(entity.GetProperties()[propertyName], expected);
        }

    }
}
